package com.iseastar.guojiang.order;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;

import com.iseastar.BaseActivity2;
import com.iseastar.guojiang.app.AppCache;
import com.iseastar.guojiang.consts.MsgID;
import com.iseastar.guojiang.logic.AppHttp;
import com.iseastar.guojiang.logic.JSON;
import com.iseastar.guojiang.model.ReqResult;
import com.shouhuobao.station.R;

import java.io.Serializable;

import droid.frame.activity.title.TitleRes;
import droid.frame.ui.dialog.AppDialog;

/**
 * 取消订单
 * 
 * @author kaka<br>
 *         2016-12-23下午2:25:02
 */
public class OrderCancelReasonActivity extends BaseActivity2 implements OnClickListener {
	/**
	 * 订单id，Intent传过来
	 */
	private Serializable orderId;
	/**
	 * 取消订单类型
	 */
	private int cancelType = 0;
	/**
	 * 其他原因
	 */
	private EditText mReasonEdit;

	@Override
	@SuppressLint("ResourceAsColor")
	protected void onCreate(Bundle savedInstanceState) {
		orderId = getIntent().getSerializableExtra("orderId");
		super.onCreate(savedInstanceState);
	}

	@Override
	protected void findViewById() {
		setContentView(R.layout.order_cancel_reason);
		super.findViewById();
		getAppTitle().setCommonTitle("选择取消原因", new TitleRes("提交", new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				if (cancelType == 0) {
					showToast("请选择取消原因");
					return;
				}
				AppDialog dialog = new AppDialog(getContext());
				dialog.setContent("确定取消订单?");
				dialog.setCancelClickListener("不取消了", null);
				dialog.setConfirmClickListener("确定取消", new View.OnClickListener() {
					@Override
					public void onClick(View v) {
						showLoadingDialog("取消订单");
						int userId = AppCache.getUser().getId();
						AppHttp.getInstance().orderCancel(userId, orderId, cancelType, mReasonEdit.getText().toString());
					}
				});
				dialog.show();
			}
		}));
		findViewById(R.id.order_cancel_1).setOnClickListener(this);
		findViewById(R.id.order_cancel_2).setOnClickListener(this);
		findViewById(R.id.order_cancel_3).setOnClickListener(this);
		findViewById(R.id.order_cancel_4).setOnClickListener(this);
		findViewById(R.id.order_cancel_5).setOnClickListener(this);
		mReasonEdit = (EditText) findViewById(R.id.order_cancel_edit);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.order_cancel_1:
			cancelType = 1;
			mReasonEdit.setVisibility(View.GONE);
			break;
		case R.id.order_cancel_2:
			cancelType = 2;
			mReasonEdit.setVisibility(View.GONE);
			break;
		case R.id.order_cancel_3:
			cancelType = 3;
			mReasonEdit.setVisibility(View.GONE);
			break;
		case R.id.order_cancel_4:
			cancelType = 4;
			mReasonEdit.setVisibility(View.GONE);
			break;
		case R.id.order_cancel_5:
			cancelType = 5;
			mReasonEdit.setVisibility(View.VISIBLE);
			break;
		default:
			break;
		}

	}

	@Override
	public boolean handleMessage(Message msg) {
		switch (msg.what) {
		case MsgID.order_cancel:
			cancelLoadingDialog();
			ReqResult<?> result = JSON.parser(msg.obj);
			if (checkLoginStatus(result)) {
				Intent data = new Intent();
				setResult(RESULT_OK, data);
				finish();
			} else {
				showToast(result.getMessage());
			}
			return false;
		default:
			break;
		}
		return true;
	}

}
